from flask import Flask,render_template
from bokeh.embed import components
from bokeh.resources import INLINE
import picture as pics
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Map,Pie
import json

app = Flask(__name__)


@app.route('/')
def index():
    html = render_template('home.html')
    return html

@app.route('/map1')
def map_1():
    with open('static/data/高校信息.json', 'r', encoding='utf-8') as f:
        data = json.load(f)
    province_name = [i['province_name'] for i in data['schools']]

    c = (
        Map()
            .add("高校", [[p[:-1], pics.sum_values(p)] for p in province_name], "china")
            .set_global_opts(
            title_opts=opts.TitleOpts(title="全国高校各省份分布图"),
            visualmap_opts=opts.VisualMapOpts(
                max_=150,
                is_piecewise=True
            ),
            legend_opts=opts.LegendOpts(
                pos_left=20,
                pos_top=100,
            ),
            toolbox_opts=opts.ToolboxOpts(
                pos_right=20,
                pos_top=20
            )
        )
    )

    c.render('templates/map1.html')

    with open("templates/map1.html", encoding="utf8", mode="r") as f:
        plot_all = "".join(f.readlines())

    return render_template('pics1.html',the_plot_all=plot_all,the_title="全国高校分布图")

@app.route('/map2')
def map_2():
    greatuni = pd.read_csv('static/data/985.csv')
    各省份985高校数量 = [i for i in greatuni['地区'].value_counts().items()]  # value_counts()用于提取重复字段并按数量从多到少排序，再转成列表包含元组的形式
    map1 = (
        Map()  # 导入pyecharts.charts中的Map
            .add("各省份985高校数量", [list(i) for i in 各省份985高校数量], "china")  # 可以指定各国家，省份
            # 上语句中传入数据必须是列表内包含列表，所以需要将以上列表包含元组进行转换
            .set_global_opts(
            title_opts=opts.TitleOpts(title="全国985高校各省份分布图"),
            legend_opts=opts.LegendOpts(is_show=False),  # 是否显示图例
            visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),  # is_piecewise=True表示使用分段显示（而不是手动拖拽范围）
        )
    )
    map1.render("templates/map2.html")

    with open("templates/map2.html", encoding="utf8", mode="r") as f:
        plot_all = "".join(f.readlines())

    return render_template('pics2.html',the_plot_all=plot_all,the_title="全国985高校各省份分布图")

@app.route('/pie')
def piec():
    globaluni = pd.read_csv('static/data/全球大学排名.csv')
    global_2016 = globaluni[(globaluni['year'] == 2016)]
    global_counts = len(global_2016['university_name'])
    district = ['China', 'Hong Kong', 'Macau', 'Taiwan']
    China_2016 = global_2016[global_2016.country.isin(district)]
    China_counts = len(China_2016['university_name'])
    global_count = global_counts - China_counts
    label = ['中国大学', '别国大学']
    count = [China_counts, global_count]

    pie = (
        Pie()
            .add("", [list(i) for i in zip(label, count)])
            .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )

    pie.render('templates/pie.html')

    with open("templates/pie.html", encoding="utf8", mode="r") as f:
        plot_all = "".join(f.readlines())

    return render_template('pics3.html',the_plot_all=plot_all,the_title="中国高校世界占比")

@app.route('/unicounts')
def unicount():
    p = pics.uni()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics4.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="普通高校数量"
    )
    return html

@app.route('/higtcounts')
def highcount():
    p = pics.high()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics5.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="普通高中数量"
    )
    return html

@app.route('/middlecounts')
def middlecount():
    p = pics.middle()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics6.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="普通初中数量"
    )
    return html

@app.route('/otherscounts')
def otherscount():
    p = pics.others()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics7.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="其余学校数量"
    )
    return html

@app.route('/recruit')
def recruit_students():
    p = pics.student()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics8.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="各类学校招生数量"
    )
    return html

@app.route('/inschool')
def inschool_students():
    p = pics.in_student()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics9.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="各类学校在校生数量"
    )
    return html

@app.route('/teachercounts')
def teachercount():
    p = pics.teachers()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics10.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="教师数量"
    )
    return html

@app.route('/propotion')
def tea_stu():
    p = pics.percent()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics11.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="师生比例情况"
    )
    return html

@app.route('/money')
def edu_fund():
    p = pics.fund()

    js_resources = INLINE.render_js()
    css_resources = INLINE.render_css()
    script, div = components(p)

    html = render_template(
        'pics12.html',
        plot_script=script,
        plot_div=div,
        js_resources=js_resources,
        css_resources=css_resources,
        the_title="教育经费情况"
    )
    return html

if __name__ == '__main__':
    app.run()
